
* This do file creates several stats on contributions starting from the final datasets created
* in Data1_Clean_Datasets.do and from the universe of contributions

clear all
set more off
cap log close
    
global dir "/Replication Archive"
cd "${dir}"


**************************************************************************
**************************************************************************
*************     	   STATS CITED IN TEXT OF PAPER     	 *************
**************************************************************************
**************************************************************************

use "${dir}/Data/Final Datasets/Directors_Information.dta", clear

unique DirectorID 
* Number of unique values of DirectorID is  401,557

unique CompanyID 
* Number of unique values of CompanyID is  14,807

* Which type of organizations enter the sample?
foreach x in Director_Current_Board Director_Historic_Board_1 Director_Historic_Board_2 Director_Current_NONBoard Director_Historic_NONBoard_1 Director_Historic_NONBoard_2 {	
		use "${dir}/Data/Boardex/Director Profile/`x'.dta", clear
		drop if DirectorName=="UnKnown"
		keep DirectorID CompanyID CompanyType
		duplicates drop DirectorID CompanyID CompanyType, force
		destring DirectorID, replace
		save "${dir}/Data/Boardex/Director Profile/`x'_temp.dta", replace
		}
		
foreach x in SMDEs_Current_Board SMDEs_Historic_Board SMDEs_Current_NONBoard SMDEs_Historic_NONBoard1 SMDEs_Historic_NONBoard2 SMDEs_Historic_NONBoard3 SMDEs_Historic_NONBoard4 SMDEs_Historic_NONBoard5 {		
		use "${dir}/Data/Boardex/SMDEs Profile/`x'.dta", clear
		drop if DirectorName=="UnKnown"
		keep DirectorID CompanyID CompanyType
		duplicates drop DirectorID CompanyID CompanyType, force
		destring DirectorID, replace
		save "${dir}/Data/Boardex/SMDEs Profile/`x'_temp.dta", replace
		}
use "${dir}/Data/Final Datasets/Directors_Information.dta", clear
foreach x in Director_Current_Board Director_Historic_Board_1 Director_Historic_Board_2 Director_Current_NONBoard Director_Historic_NONBoard_1 Director_Historic_NONBoard_2 {	
merge m:1 DirectorID CompanyID using "${dir}/Data/Boardex/Director Profile/`x'_temp.dta", update
drop if _m==2
drop _m
}
foreach x in SMDEs_Current_Board SMDEs_Historic_Board SMDEs_Current_NONBoard SMDEs_Historic_NONBoard1 SMDEs_Historic_NONBoard2 SMDEs_Historic_NONBoard3 SMDEs_Historic_NONBoard4 SMDEs_Historic_NONBoard5 {		
merge m:1 DirectorID CompanyID using "${dir}/Data/Boardex/SMDEs Profile/`x'_temp.dta", update
drop if _m==2
drop _m
}

* Number of public companies (section 2.1)
gen public=(CompanyType=="Quoted")
preserve
duplicates drop CompanyID, force
tab public
restore

* Number of companies in the sample over time (section 2.1)
preserve
duplicates drop CompanyID cycle, force
table cycle, c(n CompanyID)
restore


* Which type of organizations enter the full employment histories?
foreach x in Director_Current_Board Director_Historic_Board_1 Director_Historic_Board_2 Director_Current_NONBoard Director_Historic_NONBoard_1 Director_Historic_NONBoard_2 {	
		use "${dir}/Data/Boardex/Director Profile/`x'.dta", clear
		drop if DirectorName=="UnKnown"
		keep DirectorID CompanyID CompanyType
		duplicates drop DirectorID CompanyID CompanyType, force
		destring DirectorID, replace
		save "${dir}/Data/Boardex/Director Profile/`x'_temp.dta", replace
		}
		
foreach x in SMDEs_Current_Board SMDEs_Historic_Board SMDEs_Current_NONBoard SMDEs_Historic_NONBoard1 SMDEs_Historic_NONBoard2 SMDEs_Historic_NONBoard3 SMDEs_Historic_NONBoard4 SMDEs_Historic_NONBoard5 {		
		use "${dir}/Data/Boardex/SMDEs Profile/`x'.dta", clear
		drop if DirectorName=="UnKnown"
		keep DirectorID CompanyID CompanyType
		duplicates drop DirectorID CompanyID CompanyType, force
		destring DirectorID, replace
		save "${dir}/Data/Boardex/SMDEs Profile/`x'_temp.dta", replace
		}
		
use "${dir}/Data/Boardex/Directors_to_Match.dta", clear		
keep CompanyID DirectorID		
		
foreach x in Director_Current_Board Director_Historic_Board_1 Director_Historic_Board_2 Director_Current_NONBoard Director_Historic_NONBoard_1 Director_Historic_NONBoard_2 {	
		append using "${dir}/Data/Boardex/Director Profile/`x'_temp.dta"
		}
foreach x in SMDEs_Current_Board SMDEs_Historic_Board SMDEs_Current_NONBoard SMDEs_Historic_NONBoard1 SMDEs_Historic_NONBoard2 SMDEs_Historic_NONBoard3 SMDEs_Historic_NONBoard4 SMDEs_Historic_NONBoard5 {		
		append using "${dir}/Data/Boardex/SMDEs Profile/`x'_temp.dta"
		}			

preserve
use "${dir}/Data/Final Datasets/Directors_Information.dta", clear
keep DirectorID
duplicates drop DirectorID, force
unique DirectorID
tempfile temp
save "`temp'" , replace
restore
merge m:1 DirectorID using "`temp'"
keep if _m==3
drop _m

* Number of unique organizations in the employment histories (section 2.1)	
duplicates drop CompanyID, force
unique CompanyID
tab CompanyType, sort


* Average number of organizations with which the corporate leaders in the sample have been affiliated during their career (section 2.1)	
preserve
use "${dir}/Data/Final Datasets/Directors_Information.dta", clear
keep DirectorID
duplicates drop DirectorID, force
tempfile temp
save "`temp'" , replace
restore 
use "${dir}/Data/Boardex/Directors_to_Match_Complete_History.dta", clear
merge m:1 DirectorID using "`temp'"
keep if _m==3
drop _m
bys DirectorID: gen N=_N
duplicates drop DirectorID, force
su N, d

* Where is identification coming from? (section 3)
use "${dir}/Data/Final Datasets/Directors_Information.dta", clear
duplicates drop DirectorID catorder, force
bys DirectorID: gen N=_N
duplicates drop DirectorID, force
tab N
/*

          N |      Freq.     Percent        Cum.
------------+-----------------------------------
          1 |    355,101       88.43       88.43
          2 |     34,719        8.65       97.08
          3 |      7,582        1.89       98.97
          4 |      2,461        0.61       99.58
          5 |      1,005        0.25       99.83
          6 |        403        0.10       99.93
          7 |        167        0.04       99.97
          8 |         67        0.02       99.99
          9 |         30        0.01       99.99
         10 |         10        0.00      100.00
         11 |          8        0.00      100.00
         12 |          1        0.00      100.00
         13 |          1        0.00      100.00
         15 |          1        0.00      100.00
         22 |          1        0.00      100.00
------------+-----------------------------------
      Total |    401,557      100.00
*/
	  
use "${dir}/Data/Federal Committee Assignmnents/committee_assignments_final.dta", clear
drop if committee_1==.
bys icpsr2 chamber: gen N=_N
reshape long committee_ chairman_ ranking_minority_, i(icpsr2 cycle chamber ) j(committee_code)
keep if committee_!=.
drop committee_code
rename committee_ committee
duplicates drop icpsr2 cycle chamber committee, force
bys icpsr2 chamber committee : gen Nc=_N
gen less=(Nc<N)
preserve
duplicates drop icpsr2 chamber committee, force
tab less
/*
       less |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |      1,994       44.77       44.77
          1 |      2,460       55.23      100.00
------------+-----------------------------------
      Total |      4,454      100.00

*/
restore




***********************************************************************************
***********************************************************************************
************ TABLE 1: SUMMARY STATISTICS FOR INDIVIDUALS IN THE SAMPLE ************
***********************************************************************************
***********************************************************************************

*****************************************************************
*****************************************************************
*** 	CALCULATE ALL DONATIONS BY UNIVERSE OF DONORS 		  ***
*****************************************************************
*****************************************************************

forvalues d=2000(2)2014 {
cd "${dir}/Data/DIME_V3"
insheet using contribDB_`d'.csv, clear

* keep if individual (versus organization or committee) 
keep if contributortype=="I"

* Self-donations
drop if transactiontype =="15C"
drop if transactiontype =="16C"
drop if transactiontype =="16H"
drop if transactiontype =="16K"
drop if transactiontype =="17Z"

* Missing seats
preserve
gen miss=(seat=="")
bys bonicarid cycle: egen max=max(miss)
keep if max==1
drop max
duplicates drop seat bonicarid cycle, force
bys bonicarid cycle: gen N=_N
keep if N==2
drop N
spread seat, by(bonicarid cycle)
keep bonicarid cycle seat
duplicates drop seat bonicarid cycle, force
save "temp.dta", replace
restore
merge m:1 bonicarid cycle using "temp.dta", update
erase "temp.dta"

* Categorize races
replace seat="state:lower" if seat=="State Representative" 
replace seat="state:upper" if seat=="State Senator" 

keep seat amount cycle bonicarid

collapse (sum) amount , by(cycle seat)

save "${dir}/Data/Contributions/temp_summ_`d'.dta", replace
}


foreach i in a b c d e f g h i j k {
	cd "${dir}/Data/DIME_V3/2016"
	insheet using "xa`i'.csv", clear
	cap rename (v1-v46) (cycle transactionid transactiontype amount date bonicacid contributorname contributorlname contributorfname contributormname contributorsuffix contributortitle contributorffname contributortype contributorgender contributoraddress contributorcity contributorstate contributorzipcode contributoroccupation contributoremployer iscorp recipientname bonicarid recipientparty recipienttype recipientstate seat electiontype latitude longitude gisconfidence contributordistrict90s contributordistrict00s contributordistrict10s censustract efecmemo efecmemo2 efectransactionidorig bkreftransactionid efecorgorig efeccomidorig efecformtype excludedfromscaling contributorcfscore candidatecfscore)

* keep if individual (versus organization or committee) 
keep if contributortype=="I"

* Self-donations
drop if transactiontype =="15C"
drop if transactiontype =="16C"
drop if transactiontype =="16H"
drop if transactiontype =="16K"
drop if transactiontype =="17Z"

* Missing seats
preserve
gen miss=(seat=="")
bys bonicarid cycle: egen max=max(miss)
keep if max==1
drop max
duplicates drop seat bonicarid cycle, force
bys bonicarid cycle: gen N=_N
keep if N==2
drop N
spread seat, by(bonicarid cycle)
keep bonicarid cycle seat
duplicates drop seat bonicarid cycle, force
save "temp.dta", replace
restore
merge m:1 bonicarid cycle using "temp.dta", update
erase "temp.dta"

* Categorize races
replace seat="state:lower" if seat=="State Representative" 
replace seat="state:upper" if seat=="State Senator" 

keep seat amount cycle bonicarid

collapse (sum) amount , by(cycle seat)

save "${dir}/Data/Contributions/temp_summ_2016_`i'.dta", replace
}

use "${dir}/Data/Contributions/temp_summ_2016_a.dta", clear
foreach i in b c d e f g h i j k {
append using "${dir}/Data/Contributions/temp_summ_2016_`i'.dta"
}
collapse (sum) amount , by(cycle seat)
save "${dir}/Data/Contributions/temp_summ_2016.dta"


foreach i in a b c d f {
	cd "${dir}/Data/DIME_V3/2018"
	insheet using "xa`i'.csv", clear
	cap rename (v1-v46) (cycle transactionid transactiontype amount date bonicacid contributorname contributorlname contributorfname contributormname contributorsuffix contributortitle contributorffname contributortype contributorgender contributoraddress contributorcity contributorstate contributorzipcode contributoroccupation contributoremployer iscorp recipientname bonicarid recipientparty recipienttype recipientstate seat electiontype latitude longitude gisconfidence contributordistrict90s contributordistrict00s contributordistrict10s censustract efecmemo efecmemo2 efectransactionidorig bkreftransactionid efecorgorig efeccomidorig efecformtype excludedfromscaling contributorcfscore candidatecfscore)

* keep if individual (versus organization or committee) 
keep if contributortype=="I"

* Self-donations
drop if transactiontype =="15C"
drop if transactiontype =="16C"
drop if transactiontype =="16H"
drop if transactiontype =="16K"
drop if transactiontype =="17Z"

* Missing seats
preserve
gen miss=(seat=="")
bys bonicarid cycle: egen max=max(miss)
keep if max==1
drop max
duplicates drop seat bonicarid cycle, force
bys bonicarid cycle: gen N=_N
keep if N==2
drop N
spread seat, by(bonicarid cycle)
keep bonicarid cycle seat
duplicates drop seat bonicarid cycle, force
save "temp.dta", replace
restore
merge m:1 bonicarid cycle using "temp.dta", update
erase "temp.dta"

* Categorize races
replace seat="state:lower" if seat=="State Representative" 
replace seat="state:upper" if seat=="State Senator" 

keep seat amount cycle bonicarid

collapse (sum) amount , by(cycle seat)

save "${dir}/Data/Contributions/temp_summ_2018_`i'.dta", replace
}


foreach i in e g h i j k l m n {
	cd "${dir}/Data/DIME_V3/2018"
	insheet using "xa`i'.csv", clear
	cap rename (v1-v46) (cycle transactionid transactiontype amount date bonicacid contributorname contributorlname contributorfname contributormname contributorsuffix contributortitle contributorffname contributortype contributorgender contributoraddress contributorcity contributorstate contributorzipcode contributoroccupation contributoremployer iscorp recipientname bonicarid recipientparty recipienttype recipientstate seat electiontype latitude longitude gisconfidence contributordistrict90s contributordistrict00s contributordistrict10s censustract efecmemo efecmemo2 efectransactionidorig bkreftransactionid efecorgorig efeccomidorig efecformtype excludedfromscaling contributorcfscore candidatecfscore)

* keep if individual (versus organization or committee) 
keep if contributortype=="I"

* Self-donations
drop if transactiontype =="15C"
drop if transactiontype =="16C"
drop if transactiontype =="16H"
drop if transactiontype =="16K"
drop if transactiontype =="17Z"

* Missing seats: no observations for these subsets of data

* Categorize races
replace seat="state:lower" if seat=="State Representative" 
replace seat="state:upper" if seat=="State Senator" 

keep seat amount cycle bonicarid

collapse (sum) amount , by(cycle seat)

save "${dir}/Data/Contributions/temp_summ_2018_`i'.dta", replace
}


use "${dir}/Data/Contributions/temp_summ_2018_a.dta", clear
foreach i in b c d e f g h i j k l m n{
append using "${dir}/Data/Contributions/temp_summ_2018_`i'.dta"
}
collapse (sum) amount , by(cycle seat)
save "${dir}/Data/Contributions/temp_summ_2018.dta"



* Append all together
use "${dir}/Data/Contributions/temp_summ_2000.dta", clear
forvalues d=2002(2)2018 {
append using "${dir}/Data/Contributions/temp_summ_`d'.dta"
}

* Categorize races
gen type_donation=""
replace type_donation="Federal - Congress" if seat=="federal:house" | seat=="federal:senate"
replace type_donation="Federal - President" if seat=="federal:president" 
replace type_donation="Federal - PAC" if seat=="federal:committee" | seat=="federal:527"
replace type_donation="State - Legislative" if seat=="state:lower" | seat=="state:upper" 
replace type_donation="State - Legislative" if seat=="state senate" | seat=="state house" 
replace type_donation="State - Governor" if seat=="state:governor" 
replace type_donation="State - PAC" if seat=="state:committee"
replace type_donation="State - Other" if regexm(seat,"^(state:)") & type_donation==""
replace type_donation="Other" if type_donation==""

gen federal=(type_donation=="Federal - Congress" | type_donation=="Federal - PAC" | type_donation=="Federal - President") 
gen state=(type_donation=="State - Legislative" | type_donation=="State - Governor" | type_donation=="State - PAC")

gen amount_federal=amount*federal
gen amount_state=amount*state

egen total_federal_DIME=total(amount_federal)
egen total_state_DIME=total(amount_state)
egen total_all_races_DIME=total(amount)

save "${dir}/Data/Final Datasets/Summary_Universe_Contributions_Bonica.dta", replace

forvalues d=2000(2)2018 {
erase "${dir}/Data/Contributions/temp_summ_`d'.dta"
}
foreach i in a b c d e f g h i j k {
erase "${dir}/Data/Contributions/temp_summ_2016_`i'.dta"
}
foreach i in a b c d e f g h i j k l m n{
erase "${dir}/Data/Contributions/temp_summ_2018_`i'.dta"
}


*****************************************************************
*****************************************************************
*** CALCULATE DONATIONS TO SITTING MCs OF UNIVERSE OF DONORS  ***
*****************************************************************
*****************************************************************


insheet using "${dir}/Data/DIME_V3/dime_recipients_all_1979_2018.csv", clear
keep bonicarid icpsr2 cycle beforeswitchicpsr afterswitchicpsr name
duplicates drop bonicarid icpsr2 cycle, force

/*
REPLACE THESE MISTAKES IN BONICA DATA: different icpsr2 and bonicarid assigned to the same person
*/
replace icpsr2="PA143065" if bonicarid=="cand142752"
replace bonicarid="cand101855" if bonicarid=="cand142752"
/*
REPLACE THESE MISTAKES IN BONICA DATA FOR CANDIDATES: same bonicarid assigned to 2 different candidates
* NOTE: I do this only for candidate ever in Congress, so with a "proper" icpsr2
*/
replace bonicarid="cand1383_2" if bonicarid=="cand1383" & icpsr2=="10788"
replace bonicarid="cand767_2" if bonicarid=="cand767" & icpsr2=="10719"
replace bonicarid="cand1318_2" if bonicarid=="cand1318" & icpsr2=="21344"
replace bonicarid="cand1318_2" if bonicarid=="cand1318" & name=="maloney, sean patrick"
replace bonicarid="cand1318_2" if bonicarid=="cand1318" & name=="MALONEY, SEAN PATRICK"
replace bonicarid="cand1318_2" if bonicarid=="cand1318" & name=="friends of maloney"
replace icpsr2="21344" if bonicarid=="cand1318_2"
replace icpsr2=icpsr if bonicarid=="cand1267" & name=="BROWN, ALVIN"
replace icpsr2=icpsr if bonicarid=="cand1267" & name=="BROWN, PRINCE"
replace icpsr2=icpsr if bonicarid=="cand1267" & name=="brown, prince"
replace bonicarid="cand1267_2" if bonicarid=="cand1267" & name=="BROWN, ALVIN"
replace bonicarid="cand1267_3" if bonicarid=="cand1267" & name=="BROWN, PRINCE"
replace bonicarid="cand1267_3" if bonicarid=="cand1267" & name=="brown, prince"
replace icpsr2=icpsr if bonicarid=="cand43796" & name=="meng, matt"
replace bonicarid="cand43796_2" if bonicarid=="cand43796" & name=="meng, matt"
replace icpsr2=icpsr if bonicarid=="cand3314" & name=="santana, alberto l"
replace icpsr2=icpsr if bonicarid=="cand3314" & name=="santana, alberto"
replace bonicarid="cand3314_2" if bonicarid=="cand3314" & name=="santana, alberto l"
replace bonicarid="cand3314_2" if bonicarid=="cand3314" & name=="santana, alberto"
replace icpsr2="H4NY12029" if name=="VELAZQUEZ, PEDRO L" & bonicarid=="H4NY12029"
replace bonicarid="cand131777" if name=="VELAZQUEZ, PEDRO L" & bonicarid=="H4NY12029"
replace icpsr2="41700" if bonicarid=="cand131015" & name=="MASTO, CATHERINE CORTEZ"
replace bonicarid="cand83453" if bonicarid=="cand131015" & name=="MASTO, CATHERINE CORTEZ"
replace icpsr2="H8WY00072" if bonicarid=="cand105249" & name=="SHARRATT, BRYAN EDWARDS"
replace bonicarid="cand57085" if bonicarid=="cand105249" & name=="SHARRATT, BRYAN EDWARDS"
replace icpsr2=icpsr if bonicarid=="cand181237" & name=="bartlett, dewey follett"
replace bonicarid="cand181237_2" if bonicarid=="cand181237" & name=="bartlett, dewey follett"
replace icpsr2=icpsr if bonicarid=="cand181237" & name=="bartlett, dewey f jr"
replace bonicarid="cand181237_2" if bonicarid=="cand181237" & name=="bartlett, dewey f jr"
replace icpsr2=icpsr if bonicarid=="cand241" & name=="early, joseph d jr"
replace bonicarid="cand241_2" if bonicarid=="cand241" & name=="early, joseph d jr"
replace icpsr2=icpsr if bonicarid=="cand271" & name=="MCDONALD, KATHRYN JACKSON"
replace bonicarid="cand271_2" if bonicarid=="cand271" & name=="MCDONALD, KATHRYN JACKSON"
replace icpsr2=icpsr if bonicarid=="cand323" & name=="corcoran, thomas e"
replace bonicarid="cand323_2" if bonicarid=="cand323" & name=="corcoran, thomas e"
replace icpsr2=icpsr if bonicarid=="cand323" & name=="CORCORAN, THOMAS E"
replace bonicarid="cand323_2" if bonicarid=="cand323" & name=="CORCORAN, THOMAS E"
replace icpsr2=icpsr if bonicarid=="cand393" & name=="ATKINSON, FREDERICK WAYNE"
replace bonicarid="cand393_2" if bonicarid=="cand393" & name=="ATKINSON, FREDERICK WAYNE"
replace icpsr2="H0FL18017" if bonicarid=="cand507" & name=="EVANS, CHARLES RAY"
replace bonicarid="cand36009" if bonicarid=="cand507" & name=="EVANS, CHARLES RAY"
replace icpsr2=icpsr if bonicarid=="cand551" & name=="smith, dennis c w"
replace bonicarid="cand551_2" if bonicarid=="cand551" & name=="smith, dennis c w"
replace icpsr2=icpsr if bonicarid=="cand551" & name=="SMITH, DENNIS C W"
replace bonicarid="cand551_2" if bonicarid=="cand551" & name=="SMITH, DENNIS C W"
replace icpsr2=icpsr if bonicarid=="cand839" & name=="lynch, stephen a"
replace bonicarid="cand839_2" if bonicarid=="cand839" & name=="lynch, stephen a"
replace icpsr2="CA_CP1356482" if bonicarid=="cand1082" & name=="chu, kansen"
replace bonicarid="cand9996708" if bonicarid=="cand1082" & name=="chu, kansen"
replace icpsr2=icpsr if bonicarid=="cand1220" & name=="ANDREWS, CAMILLE SPINELLO"
replace bonicarid="cand1220_2" if bonicarid=="cand1220" & name=="ANDREWS, CAMILLE SPINELLO"
replace icpsr2=icpsr if bonicarid=="cand1407" & name=="stockman, draft"
replace bonicarid="cand1407_2" if bonicarid=="cand1407" & name=="stockman, draft"
replace icpsr2=icpsr if bonicarid=="cand1521" & name=="fletcher, wynetta wendy"
replace bonicarid="cand1521_2" if bonicarid=="cand1521" & name=="fletcher, wynetta wendy"
replace icpsr2=icpsr if bonicarid=="cand1620" & name=="faircloth, resson oliver jr"
replace bonicarid="cand1620_2" if bonicarid=="cand1620" & name=="faircloth, resson oliver jr"
replace icpsr2=icpsr if bonicarid=="cand3303" & name=="kearns, don"
replace bonicarid="cand3303_2" if bonicarid=="cand3303" & name=="kearns, don"

/*
REPLACE THESE MISTAKES IN BONICA DATA: different bonicarid assigned to the same person
* NOTE: I do this only for candidate ever in Congress, so with a "proper" icpsr2
*/
replace bonicarid="cand44995" if bonicarid=="cand140862"
replace bonicarid="cand160674" if icpsr2=="21525" & cycle==2020

duplicates drop bonicarid, force
drop name cycle
save "temp_recipient.dta", replace

use "${dir}/Data/Federal Committee Assignmnents/committee_assignments_final.dta", clear
keep icpsr2 cycle
duplicates drop icpsr2 cycle, force
destring icpsr2, replace
save "temp.dta", replace

forvalues d=2000(2)2014 {

insheet using "${dir}/Data/DIME_V3/contribDB_`d'.csv", clear

* keep if individual (versus organization or committee) 
keep if contributortype=="I"

keep seat amount cycle bonicarid transactiontype

merge m:1 bonicarid using "temp_recipient.dta"
drop if _m==2
drop _m
* Party switchers
replace icpsr2="20327" if icpsr2=="90327"
replace icpsr2="29440" if icpsr2=="29940"
replace icpsr2="29735" if icpsr2=="41105"
replace icpsr2="20101" if icpsr2=="41100"
replace icpsr2="20524" if icpsr2=="21169"
replace icpsr2="29542" if icpsr2=="99542"
replace icpsr2="29767" if icpsr2=="89767"
replace icpsr2="29767" if icpsr2=="99767"
replace icpsr2="20901" if icpsr2=="90901"
replace icpsr2="14828" if icpsr2=="94828"
replace icpsr2="29722" if icpsr2=="41103"
replace icpsr2="21144" if icpsr2=="20725"
destring icpsr2, replace force

merge m:1 icpsr2 cycle using "temp.dta"
keep if _m==3
drop _m

gen self_financing_donation=0
replace self_financing_donation=1 if transactiontype =="15C"
replace self_financing_donation=1 if transactiontype =="16C"
replace self_financing_donation=1 if transactiontype =="16H"
replace self_financing_donation=1 if transactiontype =="16K"
replace self_financing_donation=1 if transactiontype =="17Z"
replace amount=0 if self_financing_donation==1
drop self_financing_donation

collapse (sum) amount , by(cycle)

save "${dir}/Data/Contributions/temp_summ_`d'.dta", replace
}

foreach i in a b c d e f g h i j k {
insheet using "${dir}/Data/DIME_V3/2016/xa`i'.csv", clear
	cap rename (v1-v46) (cycle transactionid transactiontype amount date bonicacid contributorname contributorlname contributorfname contributormname contributorsuffix contributortitle contributorffname contributortype contributorgender contributoraddress contributorcity contributorstate contributorzipcode contributoroccupation contributoremployer iscorp recipientname bonicarid recipientparty recipienttype recipientstate seat electiontype latitude longitude gisconfidence contributordistrict90s contributordistrict00s contributordistrict10s censustract efecmemo efecmemo2 efectransactionidorig bkreftransactionid efecorgorig efeccomidorig efecformtype excludedfromscaling contributorcfscore candidatecfscore)
	
* keep if individual (versus organization or committee) 
keep if contributortype=="I"

keep seat amount cycle bonicarid transactiontype

merge m:1 bonicarid using "temp_recipient.dta"
drop if _m==2
drop _m
* Party switchers
replace icpsr2="20327" if icpsr2=="90327"
replace icpsr2="29440" if icpsr2=="29940"
replace icpsr2="29735" if icpsr2=="41105"
replace icpsr2="20101" if icpsr2=="41100"
replace icpsr2="20524" if icpsr2=="21169"
replace icpsr2="29542" if icpsr2=="99542"
replace icpsr2="29767" if icpsr2=="89767"
replace icpsr2="29767" if icpsr2=="99767"
replace icpsr2="20901" if icpsr2=="90901"
replace icpsr2="14828" if icpsr2=="94828"
replace icpsr2="29722" if icpsr2=="41103"
replace icpsr2="21144" if icpsr2=="20725"
destring icpsr2, replace force

merge m:1 icpsr2 cycle using "temp.dta"
keep if _m==3
drop _m

gen self_financing_donation=0
replace self_financing_donation=1 if transactiontype =="15C"
replace self_financing_donation=1 if transactiontype =="16C"
replace self_financing_donation=1 if transactiontype =="16H"
replace self_financing_donation=1 if transactiontype =="16K"
replace self_financing_donation=1 if transactiontype =="17Z"
replace amount=0 if self_financing_donation==1
drop self_financing_donation

collapse (sum) amount , by(cycle)

save "${dir}/Data/Contributions/temp_summ_2016_`i'.dta", replace
}

use "${dir}/Data/Contributions/temp_summ_2016_a.dta", clear
foreach i in b c d e f g h i j k {
append using "${dir}/Data/Contributions/temp_summ_2016_`i'.dta"
}
collapse (sum) amount , by(cycle)
save "${dir}/Data/Contributions/temp_summ_2016.dta"


foreach i in a b c d e f g h i j k l m n{
insheet using "${dir}/Data/DIME_V3/2018/xa`i'.csv", clear
	cap rename (v1-v46) (cycle transactionid transactiontype amount date bonicacid contributorname contributorlname contributorfname contributormname contributorsuffix contributortitle contributorffname contributortype contributorgender contributoraddress contributorcity contributorstate contributorzipcode contributoroccupation contributoremployer iscorp recipientname bonicarid recipientparty recipienttype recipientstate seat electiontype latitude longitude gisconfidence contributordistrict90s contributordistrict00s contributordistrict10s censustract efecmemo efecmemo2 efectransactionidorig bkreftransactionid efecorgorig efeccomidorig efecformtype excludedfromscaling contributorcfscore candidatecfscore)

* keep if individual (versus organization or committee) 
keep if contributortype=="I"

keep seat amount cycle bonicarid transactiontype

merge m:1 bonicarid using "temp_recipient.dta"
drop if _m==2
drop _m
* Party switchers
replace icpsr2="20327" if icpsr2=="90327"
replace icpsr2="29440" if icpsr2=="29940"
replace icpsr2="29735" if icpsr2=="41105"
replace icpsr2="20101" if icpsr2=="41100"
replace icpsr2="20524" if icpsr2=="21169"
replace icpsr2="29542" if icpsr2=="99542"
replace icpsr2="29767" if icpsr2=="89767"
replace icpsr2="29767" if icpsr2=="99767"
replace icpsr2="20901" if icpsr2=="90901"
replace icpsr2="14828" if icpsr2=="94828"
replace icpsr2="29722" if icpsr2=="41103"
replace icpsr2="21144" if icpsr2=="20725"
destring icpsr2, replace force

merge m:1 icpsr2 cycle using "temp.dta"
keep if _m==3
drop _m

gen self_financing_donation=0
replace self_financing_donation=1 if transactiontype =="15C"
replace self_financing_donation=1 if transactiontype =="16C"
replace self_financing_donation=1 if transactiontype =="16H"
replace self_financing_donation=1 if transactiontype =="16K"
replace self_financing_donation=1 if transactiontype =="17Z"
replace amount=0 if self_financing_donation==1
drop self_financing_donation

collapse (sum) amount , by(cycle)

save "${dir}/Data/Contributions/temp_summ_2018_`i'.dta", replace
}

use "${dir}/Data/Contributions/temp_summ_2018_a.dta", clear
foreach i in b c d e f g h i j k l m n {
append using "${dir}/Data/Contributions/temp_summ_2018_`i'.dta"
}
collapse (sum) amount , by(cycle)
save "${dir}/Data/Contributions/temp_summ_2018.dta"


use "${dir}/Data/Contributions/temp_summ_2000.dta", clear
forvalues d=2002(2)2018 {
append using "${dir}/Data/Contributions/temp_summ_`d'.dta"
}

egen tot_MCs_DIME=total(amount)

save "${dir}/Data/Final Datasets/Summary_Universe_Contributions_Bonica_MCs.dta", replace

forvalues d=2000(2)2018 {
erase "${dir}/Data/Contributions/temp_summ_`d'.dta"
}
foreach i in a b c d e f g h i j k {
erase "${dir}/Data/Contributions/temp_summ_2016_`i'.dta"
}
foreach i in a b c d e f g h i j k l m n{
erase "${dir}/Data/Contributions/temp_summ_2018_`i'.dta"
}


*****************************************************************
*****************************************************************
***   DATASET WITH SUMMARY STATISTCS FOR CORPORATE LEADERS    ***
*****************************************************************
*****************************************************************

use "${dir}/Data/Final Datasets/Contributions_Final.dta", clear

* Fix a small number of missing information on seat of the race
replace seat="state:lower" if seat=="State Representative" 
replace seat="state:upper" if seat=="State Senator" 
gen miss=(seat=="")
bys bonicarid cycle: egen max=max(miss)
keep if max==1
drop max
duplicates drop seat bonicarid cycle, force
bys bonicarid cycle: gen N=_N
preserve
keep if N==2
drop N
spread seat, by(bonicarid cycle)
keep bonicarid cycle seat
duplicates drop seat bonicarid cycle, force
save "temp.dta", replace
restore
preserve
keep if N>2
drop N
replace seat="federal:committee" if seat=="state:committee"
duplicates drop seat bonicarid cycle, force
bys bonicarid cycle: gen N=_N
spread seat if N==2, by(bonicarid cycle)
keep if seat=="" & bonicarid!="" 
replace seat="state:governor" if seat=="" & cycle==2004 & bonicarid=="cand110286"
replace seat="state:governor" if seat=="" & cycle==2006 & bonicarid=="cand110286"
replace seat="state:governor" if seat=="" & cycle==2010 & bonicarid=="cand110286"
replace seat="state:governor" if seat=="" & cycle==2012 & bonicarid=="cand110286"
replace seat="state:lower" if seat=="" & cycle==2006 & bonicarid=="cand111027"
replace seat="state:lower" if seat=="" & cycle==2012 & bonicarid=="cand111031"
replace seat="state:lower" if seat=="" & cycle==2014 & bonicarid=="cand111031"
replace seat="state:judicial" if seat=="" & cycle==2010 & bonicarid=="cand111164"
replace seat="state:judicial" if seat=="" & cycle==2012 & bonicarid=="cand111215"
replace seat="state:lower" if seat=="" & cycle==2012 & bonicarid=="cand111693"
replace seat="state:upper" if seat=="" & cycle==2012 & bonicarid=="cand111695"
replace seat="state:upper" if seat=="" & cycle==2012 & bonicarid=="cand111712"
replace seat="state:lower" if seat=="" & cycle==2012 & bonicarid=="cand111772"
replace seat="federal:house" if seat=="" & cycle==2006 & bonicarid=="cand1502"
replace seat="federal:house" if seat=="" & cycle==2012 & bonicarid=="cand1502"
replace seat="federal:senate" if seat=="" & cycle==2010 & bonicarid=="cand1622"
replace seat="state:office" if seat=="" & cycle==2004 & bonicarid=="cand177687"
replace seat="state:office" if seat=="" & cycle==2006 & bonicarid=="cand177687"
replace seat="state:office" if seat=="" & cycle==2008 & bonicarid=="cand177687"
replace seat="state:office" if seat=="" & cycle==2010 & bonicarid=="cand177687"
replace seat="state:office" if seat=="" & cycle==2012 & bonicarid=="cand177687"
replace seat="state:office" if seat=="" & cycle==2014 & bonicarid=="cand177687"
replace seat="state:office" if seat=="" & cycle==2012 & bonicarid=="cand48709"
replace seat="state:governor" if seat=="" & cycle==2012 & bonicarid=="cand99391"
keep if seat!=""
keep bonicarid cycle seat
duplicates drop seat bonicarid cycle, force
save "temp2.dta", replace
restore

use "${dir}/Data/Final Datasets/Contributions_Final.dta", clear
replace seat="state:lower" if seat=="State Representative" 
replace seat="state:upper" if seat=="State Senator" 
merge m:1 bonicarid cycle using "temp.dta", update
drop _m
merge m:1 bonicarid cycle using "temp2.dta", update
drop _m
replace seat="state:committee" if seat=="" & recipienttype=="COMM"
erase "temp.dta"
erase "temp2.dta"

* Categorize races
gen type_donation=""
replace type_donation="Federal - Congress" if seat=="federal:house" | seat=="federal:senate"
replace type_donation="Federal - President" if seat=="federal:president" 
replace type_donation="Federal - PAC" if seat=="federal:committee" | seat=="federal:527"
replace type_donation="State - Legislative" if seat=="state:lower" | seat=="state:upper" 
replace type_donation="State - Legislative" if seat=="state senate" | seat=="state house" 
replace type_donation="State - Governor" if seat=="state:governor" 
replace type_donation="State - PAC" if seat=="state:committee"
replace type_donation="State - Other" if regexm(seat,"^(state:)") & type_donation==""
replace type_donation="Other" if type_donation==""

* save this dataset
save "temp_donations_stats.dta", replace


**** Statistics on amount of donations in 2000-2018 period ****

use "temp_donations_stats.dta", clear
drop tot_dir tot_dir_noself
keep if cycle>=2000
* Individual's total donations
bys DirectorID: egen tot_dir_noself=total(amount_noself)
* Individual's federal donations
gen amount_noself_fed=0
replace amount_noself_fed=amount_noself if type_donation=="Federal - Congress" | type_donation=="Federal - PAC" | type_donation=="Federal - President"
bys DirectorID: egen tot_dir_noself_fed=total(amount_noself_fed)
drop amount_noself_fed
* Individual's state donations
gen amount_noself_state=0
replace amount_noself_state=amount_noself if type_donation=="State - Legislative" | type_donation=="State - Governor" | type_donation=="State - PAC"
bys DirectorID: egen tot_dir_noself_state=total(amount_noself_state)
drop amount_noself_state
* Individual's donations to MCs
preserve
use "${dir}/Data/Federal Committee Assignmnents/committee_assignments_final.dta", clear
keep icpsr2 cycle
duplicates drop icpsr2 cycle, force
save "temp.dta", replace
restore
merge m:1 icpsr2 cycle using "temp.dta"
gen MC=(_m==3)
drop if _m==2
drop _m
erase "temp.dta"
gen amount_MC=0
replace amount_MC=amount_noself if MC==1
bys DirectorID: egen tot_dir_noself_MCs=total(amount_MC)
drop amount_MC
keep DirectorID tot_dir_noself tot_dir_noself_fed tot_dir_noself_state tot_dir_noself_MCs
foreach x in tot_dir_noself tot_dir_noself_fed tot_dir_noself_state tot_dir_noself_MCs {
replace `x'=0 if `x'<0 & `x'!=.
}
duplicates drop DirectorID, force
save "temp_donations_stats2.dta", replace

**** Statistics on unique candidates and PACs receiving donations in 2000-2018 period ****

use "temp_donations_stats.dta", clear
keep if cycle>=2000
* Assign an identifier for those with missing bonicarid using recipient name
preserve
gen miss=(bonicarid=="")
bys recipientname: egen keep=max(miss)
keep if keep==1
spread bonicarid, by(recipientname cycle)
egen group=group(recipientname)
tostring group, replace
replace bonicarid="TEMP"+group if bonicarid==""
keep bonicarid recipientname cycle
duplicates drop recipientname cycle, force
save "temp.dta", replace
restore
merge m:1 recipientname cycle using "temp.dta", update
drop _m
erase "temp.dta"
* Unique candidates
preserve
drop if type_donation=="Federal - PAC" | type_donation=="State - PAC" | type_donation=="State - Other" | type_donation=="Other"
bys DirectorID bonicarid: egen tot=total(amount_noself)
drop if tot<=0
duplicates drop DirectorID bonicarid, force
bys DirectorID: gen total_candidates=_N
duplicates drop DirectorID, force
keep DirectorID total_candidates
save "temp.dta", replace
restore
* Unique PACs
keep if type_donation=="Federal - PAC" | type_donation=="State - PAC"
bys DirectorID bonicarid: egen tot=total(amount_noself)
drop if tot<=0
duplicates drop DirectorID bonicarid, force
bys DirectorID: gen total_PACs=_N
duplicates drop DirectorID, force
keep DirectorID total_PACs
merge 1:1 DirectorID using "temp.dta"
replace total_candidates=0 if _m==1
replace total_PACs=0 if _m==2 
drop _m
erase "temp.dta"
save "temp_donations_stats3.dta", replace

**** Statistics on unique MCs receiving donations in 2000-2018 period ****

use "temp_donations_stats.dta", clear
keep if cycle>=2000
preserve
use "${dir}/Data/Federal Committee Assignmnents/committee_assignments_final.dta", clear
keep icpsr2 cycle
duplicates drop icpsr2 cycle, force
save "temp.dta", replace
restore
merge m:1 icpsr2 cycle using "temp.dta"
keep if _m==3
drop _m
bys DirectorID bonicarid: egen tot=total(amount_noself)
drop if tot<=0
duplicates drop DirectorID bonicarid, force
bys DirectorID: gen total_MCs=_N
duplicates drop DirectorID, force
keep DirectorID total_MCs

**** Merge all stats together
merge 1:1 DirectorID using "temp_donations_stats2.dta"
replace total_MCs=0 if _m==2
drop _m
merge 1:1 DirectorID using "temp_donations_stats3.dta"
replace total_candidates=0 if _m==1
replace total_candidates=total_MCs if _m==1 & total_MCs!=0
replace total_PACs=0 if _m==1
drop _m

erase "temp.dta"
erase "temp_donations_stats.dta"
erase "temp_donations_stats2.dta"
erase "temp_donations_stats3.dta"
save "temp.dta", replace

* Merge to dataset with all individuals

use "${dir}/Data/Final Datasets/Directors_Information.dta", clear
keep DirectorID Board
bys DirectorID: egen ever_board=max(Board)
duplicates drop DirectorID, force
drop Board
merge 1:1 DirectorID using "temp.dta"
drop if _m==2
foreach v in tot_dir_noself tot_dir_noself_fed tot_dir_noself_state tot_dir_noself_MCs total_PACs total_candidates total_MCs {
replace `v'=0 if _m==1
}
drop _m
gen donor=(tot_dir_noself>0)
gen donor_fed=(tot_dir_noself_fed>0)
gen donor_state=(tot_dir_noself_state>0)
gen donor_MC=(tot_dir_noself_MCs>0)

cd "${dir}/Paper/Tables"

* Summary statistics Panel A
su donor
local share_any : display %5.3f `r(mean)'
gen share_any=`share_any'*100
su share_any
local share_any : display %5.1f `r(mean)'
drop share_any
su donor_fed
local share_fed : display %5.3f `r(mean)'
gen share_fed=`share_fed'*100
su share_fed
local share_fed : display %5.1f `r(mean)'
drop share_fed
su donor_state
local share_state : display %5.3f `r(mean)'
gen share_state=`share_state'*100
su share_state
local share_state : display %5.1f `r(mean)'
drop share_state
su donor_MC
local share_MC : display %5.3f `r(mean)'
gen share_MC=`share_MC'*100
su share_MC
local share_MC : display %5.1f `r(mean)'
drop share_MC
preserve
collapse (sum) tot_dir_noself tot_dir_noself_fed tot_dir_noself_state tot_dir_noself_MCs
foreach x in tot_dir_noself tot_dir_noself_fed tot_dir_noself_state tot_dir_noself_MCs {
gen `x'_Bil=`x'/1000000000
su `x'_Bil, d
local `x'_TOT : display %5.2f `r(mean)'
}
restore
preserve
use "${dir}/Data/Final Datasets/Summary_Universe_Contributions_Bonica.dta", clear
foreach x in total_all_races_DIME total_federal_DIME total_state_DIME {
gen `x'_Bil=`x'/1000000000
su `x'_Bil, d
local `x'_TOT : display %5.2f `r(mean)' 
}
restore
preserve
use "${dir}/Data/Final Datasets/Summary_Universe_Contributions_Bonica_MCs.dta", clear
gen tot_MCs_DIME_Bil=tot_MCs_DIME/1000000000
su tot_MCs_DIME_Bil, d
local tot_MCs_DIME_TOT : display %5.2f `r(mean)' 
restore
gen share_CE_all=`tot_dir_noself_TOT'/`total_all_races_DIME_TOT'*100
su share_CE_all, d
local share_CE_all : display %5.2f `r(mean)'  
gen share_CE_fed=`tot_dir_noself_fed_TOT'/`total_federal_DIME_TOT'*100
su share_CE_fed, d
local share_CE_fed : display %5.2f `r(mean)'  
gen share_CE_state=`tot_dir_noself_state_TOT'/`total_state_DIME_TOT'*100
su share_CE_state, d
local share_CE_state : display %5.2f `r(mean)'  
gen share_CE_MCs=`tot_dir_noself_MCs_TOT'/`tot_MCs_DIME_TOT'*100
su share_CE_MCs, d
local share_CE_MCs : display %5.2f `r(mean)'  

* Summary statistics Panel B
foreach x in tot_dir_noself tot_dir_noself_fed tot_dir_noself_state tot_dir_noself_MCs total_MCs {
su `x', d
local mean_`x' : display %5.1fc `r(mean)'
local sd_`x' : display %5.1fc `r(sd)'
local median_`x' : display %5.0fc `r(p50)'
su `x' if `x'>0, d
local mean_`x'_C : display %5.1fc `r(mean)'
local median_`x'_C : display %5.0fc `r(p50)'
}
foreach x in total_candidates total_PACs {
su `x', d
local mean_`x' : display %5.1fc `r(mean)'
local sd_`x' : display %5.1fc `r(sd)'
local median_`x' : display %5.0fc `r(p50)'
su `x' if tot_dir_noself>0, d
local mean_`x'_C : display %5.1fc `r(mean)'
local median_`x'_C : display %5.0fc `r(p50)'
}

* Table 
file open holder using "${dir}/Results/Table_Summary.tex", write replace text
file write holder "\begin{center}\begin{tabular}{lccccc} \hline \hline " _n
file write holder "\multicolumn{6}{l}{\textbf{\textit{Panel A: Aggregate Statistics, 1999-2018 period}}} \\" _n
file write holder " & Any & Federal & State & To Members & \\" _n
file write holder " & Election & Elections  & Elections & of Congress & \\ \hline" _n
file write holder "Share corporate elites & `share_any' & `share_fed' & `share_state' & `share_MC' & \\" _n
file write holder "  who donated (\%) &  &  &  &  & \\" _n
file write holder "Total donations by & `tot_dir_noself_TOT' & `tot_dir_noself_fed_TOT' & `tot_dir_noself_state_TOT' & `tot_dir_noself_MCs_TOT' & \\" _n
file write holder "  corporate elites (\$ B) &  &  &  &  & \\" _n
file write holder "Total donations by & `total_all_races_DIME_TOT' & `total_federal_DIME_TOT' & `total_state_DIME_TOT' & `tot_MCs_DIME_TOT' & \\" _n
file write holder "  all U.S. donors (\$ B) &  &  &  &  & \\" _n
file write holder "Share of total donations & `share_CE_all' & `share_CE_fed' & `share_CE_state' & `share_CE_MCs' & \\" _n
file write holder "  by corporate elites (\%) &  &  &  &  & \\" _n
file write holder " &  &  &  &  &  \\" _n
file write holder "\multicolumn{6}{l}{\textbf{\textit{Panel B: Summary Statistics on 401,557 Corporate Elites Members}}} \\" _n
file write holder " &  &  &  & Mean & Median \\" _n
file write holder " & Mean & Std. Dev. & Median & if donated & if donated \\ \hline" _n
file write holder "Amount donated & `mean_tot_dir_noself' & `sd_tot_dir_noself' & `median_tot_dir_noself' & `mean_tot_dir_noself_C' & `median_tot_dir_noself_C' \\" _n
file write holder "Candidates supported & `mean_total_candidates' & `sd_total_candidates' & `median_total_candidates' & `mean_total_candidates_C' & `median_total_candidates_C' \\" _n
file write holder "PACs supported & `mean_total_PACs' & `sd_total_PACs' & `median_total_PACs' & `mean_total_PACs_C' & `median_total_PACs_C' \\" _n
file write holder "Amount donated federal & `mean_tot_dir_noself_fed' & `sd_tot_dir_noself_fed' & `median_tot_dir_noself_fed' & `mean_tot_dir_noself_fed_C' & `median_tot_dir_noself_fed_C' \\" _n
file write holder "Amount donated state & `mean_tot_dir_noself_state' & `sd_tot_dir_noself_state' & `median_tot_dir_noself_state' & `mean_tot_dir_noself_state_C' & `median_tot_dir_noself_state_C' \\" _n
file write holder "Amount donated MCs & `mean_tot_dir_noself_MCs' & `sd_tot_dir_noself_MCs' & `median_tot_dir_noself_MCs' & `mean_tot_dir_noself_MCs_C' & `median_tot_dir_noself_MCs_C' \\" _n
file write holder "MCs supported & `mean_total_MCs' & `sd_total_MCs' & `median_total_MCs' & `mean_total_MCs_C' & `median_total_MCs_C' \\" _n
file write holder " \end{tabular}\end{center}" _n
file close holder

cd "${dir}"

erase "temp.dta"

***********************************************************************************
***********************************************************************************
*************   	  TABLE A9: SECTOR DISTRIBUTION IN BOARDEX    	  *************
***********************************************************************************
***********************************************************************************

use "${dir}/Data/Final Datasets/Directors_Information.dta", clear
duplicates drop CompanyID, force
tabout Sector using "${dir}/Results/table_sectors_classification.xls", f(0c 2p) c(freq col) sort replace


***********************************************************************************
***********************************************************************************
*************    TABLE A10: SUMMARY STATISTICS ON CORPORATE LEADERS    *************
***********************************************************************************
***********************************************************************************
	

* Which type of organizations enter the sample?
foreach x in Director_Current_Board Director_Historic_Board_1 Director_Historic_Board_2 Director_Current_NONBoard Director_Historic_NONBoard_1 Director_Historic_NONBoard_2 {	
		use "${dir}/Data/Boardex/Director Profile/`x'.dta", clear
		drop if DirectorName=="UnKnown"
		keep DirectorID CompanyID CompanyType
		duplicates drop DirectorID CompanyID CompanyType, force
		destring DirectorID, replace
		save "${dir}/Data/Boardex/Director Profile/`x'_temp.dta", replace
		}
		
foreach x in SMDEs_Current_Board SMDEs_Historic_Board SMDEs_Current_NONBoard SMDEs_Historic_NONBoard1 SMDEs_Historic_NONBoard2 SMDEs_Historic_NONBoard3 SMDEs_Historic_NONBoard4 SMDEs_Historic_NONBoard5 {		
		use "${dir}/Data/Boardex/SMDEs Profile/`x'.dta", clear
		drop if DirectorName=="UnKnown"
		keep DirectorID CompanyID CompanyType
		duplicates drop DirectorID CompanyID CompanyType, force
		destring DirectorID, replace
		save "${dir}/Data/Boardex/SMDEs Profile/`x'_temp.dta", replace
		}

use "${dir}/Data/Final Datasets/Directors_Information.dta", clear

preserve
bys DirectorID cycle: gen N=_N
duplicates drop DirectorID cycle, force
su N, d
local mean_N: display %5.2f `r(mean)'
local sd_N: display %5.2f `r(sd)'
local median_N: display %5.0f `r(p50)'
local min_N: display %5.0f `r(min)'
local max_N: display %5.0f `r(max)'
restore

foreach x in Director_Current_Board Director_Historic_Board_1 Director_Historic_Board_2 Director_Current_NONBoard Director_Historic_NONBoard_1 Director_Historic_NONBoard_2 {	
merge m:1 DirectorID CompanyID using "${dir}/Data/Boardex/Director Profile/`x'_temp.dta", update
drop if _m==2
drop _m
}
foreach x in SMDEs_Current_Board SMDEs_Historic_Board SMDEs_Current_NONBoard SMDEs_Historic_NONBoard1 SMDEs_Historic_NONBoard2 SMDEs_Historic_NONBoard3 SMDEs_Historic_NONBoard4 SMDEs_Historic_NONBoard5 {		
merge m:1 DirectorID CompanyID using "${dir}/Data/Boardex/SMDEs Profile/`x'_temp.dta", update
drop if _m==2
drop _m
}

gen public=(CompanyType=="Quoted")
preserve
keep if public==1
bys DirectorID cycle: gen N=_N
duplicates drop DirectorID cycle, force
su N, d
local mean_Np: display %5.2f `r(mean)'
local sd_Np: display %5.2f `r(sd)'
local median_Np: display %5.0f `r(p50)'
local min_Np: display %5.0f `r(min)'
local max_Np: display %5.0f `r(max)'
restore

preserve
bys DirectorID cycle: egen max=max(Board)
duplicates drop DirectorID cycle, force
su max, d
local mean_Board: display %5.2f `r(mean)'
local sd_Board: display %5.2f `r(sd)'
local median_Board: display %5.0f `r(p50)'
local min_Board: display %5.0f `r(min)'
local max_Board: display %5.0f `r(max)'
restore

preserve
bys DirectorID cycle: egen tot_boards=total(Board)
duplicates drop DirectorID cycle, force
su tot_boards, d
local mean_tot_boards: display %5.2f `r(mean)'
local sd_tot_boards: display %5.2f `r(sd)'
local median_tot_boards: display %5.0f `r(p50)'
local min_tot_boards: display %5.0f `r(min)'
local max_tot_boards: display %5.0f `r(max)'
restore

preserve
duplicates drop DirectorID CompanyID, force
bys DirectorID: gen N=_N
duplicates drop DirectorID, force
su N, d
local mean_N2: display %5.2f `r(mean)'
local sd_N2: display %5.2f `r(sd)'
local median_N2: display %5.0f `r(p50)'
local min_N2: display %5.0f `r(min)'
local max_N2: display %5.0f `r(max)'
restore


preserve
duplicates drop DirectorID CompanyID, force
keep if public==1
bys DirectorID: gen N=_N
duplicates drop DirectorID, force
su N, d
local mean_N2p: display %5.2f `r(mean)'
local sd_N2p: display %5.2f `r(sd)'
local median_N2p: display %5.0f `r(p50)'
local min_N2p: display %5.0f `r(min)'
local max_N2p: display %5.0f `r(max)'
restore

preserve
duplicates drop DirectorID CompanyID, force
bys DirectorID: egen max=max(Board)
duplicates drop DirectorID, force
su max, d
local mean_Board2: display %5.2f `r(mean)'
local sd_Board2: display %5.2f `r(sd)'
local median_Board2: display %5.0f `r(p50)'
local min_Board2: display %5.0f `r(min)'
local max_Board2: display %5.0f `r(max)'
restore

preserve
duplicates drop DirectorID CompanyID, force
bys DirectorID: egen tot_boards=total(Board)
duplicates drop DirectorID, force
su tot_boards, d
local mean_tot_boards2: display %5.2f `r(mean)'
local sd_tot_boards2: display %5.2f `r(sd)'
local median_tot_boards2: display %5.0f `r(p50)'
local min_tot_boards2: display %5.0f `r(min)'
local max_tot_boards2: display %5.0f `r(max)'
restore

preserve
use "${dir}/Data/Boardex/Directors_to_Match_Complete_History.dta", clear
keep DirectorID CompanyID
duplicates drop DirectorID CompanyID, force
bys DirectorID: gen N=_N
drop CompanyID
duplicates drop DirectorID, force
tempfile temp
save "`temp'" , replace
restore
merge m:1 DirectorID using "`temp'"
keep if _m==3
drop _m
duplicates drop DirectorID, force
su N, d
local mean_Nall: display %5.2f `r(mean)'
local sd_Nall: display %5.2f `r(sd)'
local median_Nall: display %5.0f `r(p50)'
local min_Nall: display %5.0f `r(min)'
local max_Nall: display %5.0f `r(max)'

* Table 
file open holder using "${dir}/Results/Table_Summary_Appendix.tex", write replace text
file write holder "\begin{center}\begin{tabular}{lccccc} \hline \hline " _n
file write holder "\multicolumn{6}{l}{\textbf{\textit{Panel A: Statistics at the corporate leader - election cycle level}}} \\" _n
file write holder " & Mean & Median & Std. Dev. & Min & Max \\ hline" _n
file write holder " Total companies & `mean_N' & `median_N' & `sd_N' & `min_N' & `max_N' \\" _n
file write holder " Total public companies & `mean_Np' & `median_Np' & `sd_Np' & `min_Np' & `max_Np' \\" _n
file write holder " Board member & `mean_Board' & `median_Board' & `sd_Board' & `min_Board' & `max_Board' \\" _n
file write holder " Total board positions & `mean_tot_boards' & `median_tot_boards' & `sd_tot_boards' & `min_tot_boards' & `max_tot_boards' \\" _n
file write holder " &  &  &  &  &  \\" _n
file write holder "\multicolumn{6}{l}{\textbf{\textit{Panel B: Statistics at the corporate leader level}}} \\" _n
file write holder " & Mean & Median & Std. Dev. & Min & Max \\ hline" _n
file write holder " Total companies & `mean_N2' & `median_N2' & `sd_N2' & `min_N2' & `max_N2' \\" _n
file write holder " Total public companies & `mean_N2p' & `median_N2p' & `sd_N2p' & `min_N2p' & `max_N2p' \\" _n
file write holder " Board member & `mean_Board2' & `median_Board2' & `sd_Board2' & `min_Board2' & `max_Board2' \\" _n
file write holder " Total board positions & `mean_tot_boards2' & `median_tot_boards2' & `sd_tot_boards2' & `min_tot_boards2' & `max_tot_boards2' \\" _n
file write holder " Total employers & `mean_Nall' & `median_Nall' & `sd_Nall' & `min_Nall' & `max_Nall' \\ \hline \hline" _n
file write holder " \end{tabular}\end{center}" _n
file close holder

foreach x in Director_Current_Board Director_Historic_Board Director_Current_NONBoard Director_Historic_NONBoard {
		cap erase "${dir}/Data/Boardex/Director Profile/`x'_temp.dta"
		}
foreach x in SMDEs_Current_Board SMDEs_Historic_Board SMDEs_Current_NONBoard SMDEs_Historic_NONBoard SMDEs_Historic_NONBoard1 {		
		cap erase "${dir}/Data/Boardex/SMDEs Profile/`x'_temp.dta"
		}	

		
***************************************************************************
***************************************************************************
************   TABLE A11: MATCHING RATE AND STEPS OF MATCHING  ************
***************************************************************************
***************************************************************************

use "${dir}/Data/Final Datasets/Directors_Information.dta", clear
keep DirectorID
duplicates drop DirectorID, force
tempfile temp
save "`temp'" , replace
use "${dir}/Data/Matched Datasets/Contributions_Matched.dta", clear
merge m:1 DirectorID using "`temp'"
drop if _m==1
gen matched=(_m==3)
drop _m
* Steps in which matched?
bys DirectorID: egen first_step_matched=min(step_match)
duplicates drop DirectorID, force
forvalues i=1(1)8 {
count if first_step_matched==`i'
local step`i': display %10.0gc `r(N)'
}
count if first_step_matched==.
local step0: display %10.0gc `r(N)'

* Table 
file open holder using "${dir}/Results/Table_Matching_Appendix.tex", write replace text
file write holder "\begin{center}\begin{tabular}{cc} \hline \hline " _n
file write holder " & Matching & Number of \\ hline" _n
file write holder " & Step & Individuals Matched \\ hline" _n
file write holder "First + Middle + Last + Suffix + Company Name & `step1'  \\" _n
file write holder "First + Middle + Last + Company Name  & `step2'  \\" _n
file write holder "First + Last + Suffix + Company Name  & `step3'  \\" _n
file write holder "First + Last + Company Name  & `step4'  \\" _n
file write holder "First + Middle + Last + Suffix + Fuzzy Company Name  & `step5'  \\" _n
file write holder "First + Middle + Last + Fuzzy Company Name  & `step6'  \\" _n
file write holder "First + Last + Suffix + Fuzzy Company Name  & `step7'  \\" _n
file write holder "First + Last + Fuzzy Company Name  & `step8'  \\" _n
file write holder "Never Matched  & `step0'  \\ \hline \hline" _n
file write holder " \end{tabular}\end{center}" _n
file close holder


***************************************************************************
***************************************************************************
************   	TABLE A12: TOP 3 RELEVANT ISSUES BY SECTOR     ************
***************************************************************************
***************************************************************************

use "${dir}/Data/Lobbying CRP/lob_issue_NoSpecficIssue.dta", clear
keep issueID issue
duplicates drop issueID, force
tempfile temp
save "`temp'" , replace

use "${dir}/Data/Final Datasets/Directors_Information.dta", clear
keep industry
duplicates drop industry, force
tempfile temp_c
save "`temp_c'" , replace

use "${dir}/Data/Lobbying CRP/top_issues_definition_2.dta", clear
keep issueID industry
merge m:1 industry using "`temp_c'"
keep if _m==3
drop _m
merge m:1 issueID using "`temp'"
keep if _m==3
drop _m
replace industry=subinstr(industry, "Misc", "Miscellaneous",.)
foreach x in industry issue {
replace `x'=subinstr(`x', "&", "and",.)
}
drop issueID
bys industry: gen n=_n
reshape wide issue, i(industry) j(n)
sort industry
ssc install dataout
dataout, save("${dir}/Results/table_top3_issues") tex replace


 
		
		





